/*
 * 用户注册界面
 */
package com.wonyesoft.wuliu.acticity;

import httptools.HttpJsonTool;

import java.util.HashMap;
import java.util.Map;

import tools.InvitationCode;
import tools.SMSBroadcastReceiver;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.zxing.CaptureActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.wonyesoft.wuliu.R;
import com.wonyesoft.wuliu.acticity.base.BaseActivity;
import com.wonyesoft.wuliu.acticity.utils.HttpUtilsEx;
import com.wonyesoft.wuliu.acticity.utils.StringUtil;
import com.wonyesoft.wuliu.acticity.utils.ToastUtils;
import com.wonyesoft.wuliu.api.GagApi;
import com.wonyesoft.wuliu.vo.JsonRequest;
import com.wonyesoft.wuliu.vo.JsonResponse;
import com.wonyesoft.wuliu.vo.State;

public class UserRegisterActivity extends BaseActivity implements
		OnClickListener {

	private TabHost mTabHost;
	private RadioGroup mRadioGroup;
	private TextView title, head_right;
	private ImageView head_left;
	private Button user_reg, register_sms_ma;
	private String mobile, password, code;
	private EditText register_id, register_password, register_sms;
	private Gson gson = new Gson();
	private HttpUtils http = HttpUtilsEx.getHttpUtils();
	private RequestParams params;
	private int mReSendTime = 60;
	
	/**
	 * 用户协议查看
	 */
	boolean deal_flag=false;
	ImageButton imagebutton_register_deal;
	TextView register_deal_TextView;
	/**
	 * 短信拦截
	 */
	private SMSBroadcastReceiver mSMSBroadcastReceiver;
	private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
	@SuppressLint("NewApi")
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		System.out.println("regsiter");
		title = (TextView) findViewById(R.id.head_title);
		head_left = (ImageView) findViewById(R.id.head_left);
		head_right = (TextView) findViewById(R.id.head_right);
		register_password = (EditText) findViewById(R.id.register_password);
		register_id = (EditText) findViewById(R.id.register_id);
		user_reg = (Button) findViewById(R.id.login_denglu);
		register_sms = (EditText) findViewById(R.id.register_sms);
		register_sms_ma = (Button) findViewById(R.id.register_sms_ma);
		head_left.setVisibility(0);
		title.setText("用户注册");
	//	head_right.setVisibility(0);
	//	head_right.setText("注册");
		register_sms_ma.setOnClickListener(this);
		head_right.setOnClickListener(this);
		head_left.setOnClickListener(this);
		user_reg.setOnClickListener(this);
		startMSMReceiver();
		imagebutton_register_deal=(ImageButton) findViewById(R.id.imagebutton_register_deal);
		imagebutton_register_deal.setImageResource(R.drawable.unchecked);
		imagebutton_register_deal.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (deal_flag) {
					deal_flag=false;
					imagebutton_register_deal.setImageResource(R.drawable.unchecked);
				}else {
					deal_flag=true;
					imagebutton_register_deal.setImageResource(R.drawable.check);
				}
			}
		});
		register_deal_TextView=(TextView) findViewById(R.id.register_deal_TextView);
		register_deal_TextView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				startActivity(new Intent(UserRegisterActivity.this,GongGaoActivity.class));
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.head_left:
			finish();
			break;
		case R.id.head_right:
		case R.id.login_denglu:
			reg();
			break;
		case R.id.register_sms_ma:
			mobile = register_id.getText().toString().trim();
			if (StringUtil.isEmpty(mobile))
				showLongToast("请填写手机号码");
			else
				getCode();
			break;
		default:
			break;
		}
	}

	private void getCode() {

		HttpJsonTool httpJsonTool=new HttpJsonTool(this,true);
		httpJsonTool.SetOnLister(new Handler(){
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				if (msg.what==1) {
					showLongToast("成功获得验证码，请等待验证短信");
					goNext();
				}else {
					Toast.makeText(UserRegisterActivity.this,  "请检查您的手机号码输入是否有误或者是否重复注册", Toast.LENGTH_LONG).show();
				}
			}
		});
		
		
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("phone", InvitationCode.enCode(mobile));
		dataMap.put("type", "2");
		httpJsonTool.StartWork(dataMap, GagApi.GETCODE, null, null);

	}

	protected void goNext() {
		new CountDownTimer(60 * 1000, 1000) {
			@Override
			public void onFinish() {
				// done
				mReSendTime = 60;
				register_sms_ma.setEnabled(true);
				register_sms_ma.setText("发送验证码");
			}

			@Override
			public void onTick(long arg0) {
				// 每1000毫秒回调的方法
				register_sms_ma.setEnabled(false);
				register_sms_ma.setText("" + mReSendTime--);
			}

		}.start();
	}

	private void reg() {
		if (!deal_flag) {
			Toast.makeText(this, "请仔细阅读用户协议，后点同意！！！", Toast.LENGTH_LONG).show();
			return;
		}
		showLoadingDialog("注册中...");
		mobile = register_id.getText().toString().trim();
		password = register_password.getText().toString().trim();
		code = register_sms.getText().toString().trim();
		if ((mobile.length()!=11)) {
			Toast.makeText(this, "请填写正确的手机号码", Toast.LENGTH_SHORT).show();
			dismissLoadingDialog();
			return;
		}
		if ((password.length()<3)) {
			Toast.makeText(this, "请填写密码，密码至少3位", Toast.LENGTH_SHORT).show();
			dismissLoadingDialog();
			return;
		}
		if (code.length()<1) {
			Toast.makeText(this, "请填写验证码", Toast.LENGTH_SHORT).show();
			dismissLoadingDialog();
			return;
		}
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("phone", InvitationCode.enCode(mobile));
		dataMap.put("password", password);
		dataMap.put("code", code);
		
		HttpJsonTool httpJsonTool=new HttpJsonTool(this);
		httpJsonTool.SetOnLister(new Handler(){
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				dismissLoadingDialog();
				if (msg.what==1) {
					Toast.makeText(UserRegisterActivity.this, (String)msg.obj, Toast.LENGTH_LONG).show();
					openActivity(UserLoginActivity.class);
					//dismissLoadingDialog();
					finish();
				}else {
				//	dismissLoadingDialog();
					Toast.makeText(UserRegisterActivity.this, (String)msg.obj+",请检查验证码是否正确", Toast.LENGTH_LONG).show();
				}
			}
		});
		httpJsonTool.StartWork(dataMap, GagApi.REGISTER, null, null);

	}

	private void startMSMReceiver(){
		mSMSBroadcastReceiver=new SMSBroadcastReceiver();
		//实例化过滤器并设置要过滤的广播
        IntentFilter intentFilter = new IntentFilter(ACTION);
        intentFilter.setPriority(Integer.MAX_VALUE);
        //注册广播
        this.registerReceiver(mSMSBroadcastReceiver, intentFilter);
        Log.v("广播注册开启", "广播注册开启");
        mSMSBroadcastReceiver.setOnReceivedMessageListener(new SMSBroadcastReceiver.MessageListener() {
            @Override
            public void onReceived(String message,int type) {
            	if (type==1) {
					//验证码
            		register_sms.setText(message);
				}
            	if (type==2) {
            		//邀请码
            	//	register_team.setText(message);
				}
            }
        });
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		this.unregisterReceiver(mSMSBroadcastReceiver);
	}
}
